package pe.com.rimac.portal.tag.url;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;

import pe.com.rimac.portal.tag.PortletBaseTag;
import pe.com.rimac.portal.util.url.UrlService;
import pe.com.rimac.portal.util.url.UrlServiceFactory;

import com.ibm.portal.state.EngineURL;

public class SinglePortletRefreshTag extends PortletBaseTag implements UrlTag 
{
    private static UrlService urlService = UrlServiceFactory.getUrlService();
    
    private Map params;
    private String type;

    public int doStartTag() throws JspTagException
    {
        params = new HashMap();
        return EVAL_BODY_INCLUDE;
    }
    
    public int doEndTag() throws JspException
    {
        try 
        {
            EngineURL url;
            if ("render".equals(type))
            {
                url = urlService.createSingleRenderURL(getRenderRequest(), getRenderResponse(), params);
            }
            else
            {
                url = urlService.createSingleActionURL(getRenderRequest(), getRenderResponse(), params);
            }
            pageContext.getOut().print(url);
        }
        catch (Exception ex) 
        {
            throw new JspTagException("SinglePortletRefreshTag: " + ex.getMessage());
        }
        return EVAL_PAGE;
    }
    
    public void setType(String type)
    {
        this.type = type;
    }
    
    public void addParam(String name, String value)
    {
        this.params.put(name, new String[] { value });
    }
    
    public void addParam(String name, String[] values)
    {
        this.params.put(name, values);
    }
}